from PyQt4.QtCore import *
from PyQt4.QtGui import *

from clickFuUtils import cfAction

from settingZoom import Ui_ZoomSetting

zoomItem="/clickfu/osm/zoom"
zoomDefault=12

class osmMap(cfAction):
    def __init__(self,iface):
        cfAction.__init__(self,self.name(),iface)
        return None
    def tag(self):
        return "osmMap"
    def name(self):
        return "View OSM map"
    def desc(self):
        return "Goto Location on OpenStreetMap"

    def createURL(self,lat,long):
        url = "http://www.openstreetmap.org/?lat=%s&lon=%s&zoom=%s" % (lat,long, getZoom())
        return url


class osmeditMap(cfAction):
    def __init__(self,iface):
        cfAction.__init__(self,self.name(),iface)
        return None
    def tag(self):
        return "osmEditMap"
    def name(self):
        return "Edit OSM map"
    def desc(self):
        return "Goto Location on OpenStreetMap and start editing"

    def createURL(self,lat,long):
        url = "http://www.openstreetmap.org/edit?lat=%s&lon=%s&zoom=%s" % (lat,long,getZoom())
        return url

def getZoom():
    return QSettings().value(zoomItem,QVariant(zoomDefault)).toInt()[0]
def setZoom(n):
    QSettings().setValue(zoomItem,n)

class osmSettings(QAction):

    def __init__(self,iface):
        QAction.__init__(self,self.name(),iface.mainWindow())
        QObject.connect(self,SIGNAL("triggered()"),self.setMe)
        return None

    def name(self):
        return "OpenStreetMap"

    def zoomProperties(self):
        return {
            'min': 0,
            'max': 18,
            'order': 1
            }

    def setMe(self):
        window=QDialog()
        ui=Ui_ZoomSetting()
        ui.setupUi(window)
        ui.zoomSlider.setInvertedAppearance(False)
        ui.zoomSlider.setRange(0,18)
        ui.valueBox.setRange(0,18)

        ui.zoomSlider.setValue(getZoom())
        if window.exec_():
            setZoom(ui.zoomSlider.value())

        # osm zoom is zoom=0 (world) to zoom=18 (zoomed in)
        # could also set layers?
